#include "apiordercache.h"
#include <sstream>
#include "minutelisten.h"
#include "util.h"
#include "log.h"


static bool __init__ = []()->bool {
    MinuteListen::Instance()->Register("ApiOrderMgr" , [&](std::int64_t tick){
        ApiOrderCache::Instance()->Reset();
    });
    return true;
}();


void ApiOrderCache::CacheBetFailed(std::uint64_t order_id, Data&& data)
{
    m_bet_failed_group[order_id] = std::move(data);
}
ApiOrderCache::Data* ApiOrderCache::CheckBetFailed(std::uint64_t order_id)
{
    auto it = m_bet_failed_group.find(order_id);
    if (m_bet_failed_group.end() == it)
    {
        return nullptr;
    }
    return &it->second;
}
void ApiOrderCache::ClearBetFailed(std::uint64_t order_id)
{
    m_bet_failed_group.erase(order_id);
}
void ApiOrderCache::CacheCancleBet(std::uint64_t order_id, std::uint64_t expire_time)
{
    m_cancle_bet_group[order_id] = expire_time;
}
bool ApiOrderCache::CheckCancleBet(std::uint64_t order_id)
{
    return m_cancle_bet_group.end() != m_cancle_bet_group.find(order_id);
}
void ApiOrderCache::ClearCancleBet(std::uint64_t order_id)
{
    m_cancle_bet_group.erase(order_id);
}
void ApiOrderCache::CacheBetAndWinFailed(std::uint64_t order_id, Data&& data)
{
    m_bet_and_win_failed_group[order_id] = std::move(data);
}
ApiOrderCache::Data* ApiOrderCache::CheckBetAndWinFailed(std::uint64_t order_id)
{
    auto it = m_bet_and_win_failed_group.find(order_id);
    if (m_bet_and_win_failed_group.end() == it)
    {
        return nullptr;
    }
    return &it->second;
}
void ApiOrderCache::ClearBetAndWinFailed(std::uint64_t order_id)
{
    m_bet_and_win_failed_group.erase(order_id);
}
void ApiOrderCache::CacheCancleBetAndWin(std::uint64_t order_id, std::uint64_t expire_time)
{
    m_cancle_bet_and_win_group[order_id] = expire_time;
}
bool ApiOrderCache::CheckCancleBetAndWin(std::uint64_t order_id)
{
    return m_cancle_bet_and_win_group.end() != m_cancle_bet_and_win_group.find(order_id);
}
void ApiOrderCache::ClearCancleBetAndWin(std::uint64_t order_id)
{
    m_cancle_bet_and_win_group.erase(order_id);
}

void ApiOrderCache::Reset()
{
    // log_debug("reset");

    uint64_t now = CTools::GetUnixTime();
    for (auto it = m_bet_failed_group.begin(); it != m_bet_failed_group.end();)
    {
        if (now > it->second.expire_time)
        {
            it = m_bet_failed_group.erase(it);
        }
        else 
        {
            it++;
        }
    }
    for (auto it = m_cancle_bet_group.begin(); it != m_cancle_bet_group.end();)
    {
        if (now > it->second)
        {
            it = m_cancle_bet_group.erase(it);
        }
        else 
        {
            it++;
        }
    }
    for (auto it = m_bet_and_win_failed_group.begin(); it != m_bet_and_win_failed_group.end();)
    {
        if (now > it->second.expire_time)
        {
            it = m_bet_and_win_failed_group.erase(it);
        }
        else 
        {
            it++;
        }
    }
    for (auto it = m_cancle_bet_and_win_group.begin(); it != m_cancle_bet_and_win_group.end();)
    {
        if (now > it->second)
        {
            it = m_cancle_bet_and_win_group.erase(it);
        }
        else 
        {
            it++;
        }
    }
}













